<?php

namespace common\models;

use Yii;
use common\models\meta\ArticleMetaLike;
use common\models\meta\ArticleMetaTag;

/**
 * This is the model class for table "{{%article}}".
 *
 * @property int $id 文章自增id
 * @property int $article_category_id 文章分类id
 * @property int $type 类型.0文章,1单页
 * @property string $title 文章标题
 * @property string $sub_title 副标题
 * @property string $summary 文章概要
 * @property string $thumb 缩略图
 * @property string $seo_title seo标题
 * @property string $seo_keywords seo关键字
 * @property string $seo_description seo描述
 * @property int $status 状态.0草稿,1发布
 * @property int $sort 排序
 * @property int $author_id 发布文章管理员id
 * @property string $author_name 发布文章管理员用户名
 * @property int $scan_count 浏览次数
 * @property int $comment_count 浏览次数
 * @property int $can_comment 是否可评论.0否,1是
 * @property int $visibility 文章可见性.1.公开,2.评论可见,3.加密文章,4.登陆可见
 * @property string $password 文章明文密码
 * @property int $flag_headline 头条.0否,1.是
 * @property int $flag_recommend 推荐.0否,1.是
 * @property int $flag_slide_show 幻灯.0否,1.是
 * @property int $flag_special_recommend 特别推荐.0否,1.是
 * @property int $flag_roll 滚动.0否,1.是
 * @property int $flag_bold 加粗.0否,1.是
 * @property int $flag_picture 图片.0否,1.是
 * @property int $created_at 创建时间
 * @property int $updated_at 最后修改时间
 *
 * @property ArticleCategory $articleCategory
 * @property ArticleContent[] $articleContents
 * @property ArticleMetum[] $articleMeta
 */
class Article extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%article}}';
    }

    /**
     * {@inheritdoc}
     * @return ArticleQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new ArticleQuery(get_called_class());
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [
                [
                    'article_category_id',
                    'type',
                    'status',
                    'sort',
                    'author_id',
                    'scan_count',
                    'comment_count',
                    'can_comment',
                    'visibility',
                    'flag_headline',
                    'flag_recommend',
                    'flag_slide_show',
                    'flag_special_recommend',
                    'flag_roll',
                    'flag_bold',
                    'flag_picture',
                    'created_at',
                    'updated_at',
                ],
                'integer',
            ],
            [['title', 'created_at'], 'required'],
            [['title', 'sub_title', 'summary', 'thumb', 'seo_title', 'seo_keywords', 'seo_description', 'author_name', 'password'], 'string', 'max' => 255],
            [
                ['article_category_id'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => ArticleCategory::className(),
                'targetAttribute' => ['article_category_id' => 'id'],
            ],
            [['status', 'can_comment', 'visibility', 'sort'], 'default', 'value' => 0],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'                     => '文章自增id',
            'article_category_id'    => '文章分类id',
            'type'                   => '类型.0文章,1单页',
            'title'                  => '文章标题',
            'sub_title'              => '副标题',
            'summary'                => '文章概要',
            'thumb'                  => '缩略图',
            'seo_title'              => 'seo标题',
            'seo_keywords'           => 'seo关键字',
            'seo_description'        => 'seo描述',
            'status'                 => '状态.0草稿,1发布',
            'sort'                   => '排序',
            'author_id'              => '发布文章管理员id',
            'author_name'            => '发布文章管理员用户名',
            'scan_count'             => '浏览次数',
            'comment_count'          => '浏览次数',
            'can_comment'            => '是否可评论.0否,1是',
            'visibility'             => '文章可见性.1.公开,2.评论可见,3.加密文章,4.登陆可见',
            'password'               => '文章明文密码',
            'flag_headline'          => '头条.0否,1.是',
            'flag_recommend'         => '推荐.0否,1.是',
            'flag_slide_show'        => '幻灯.0否,1.是',
            'flag_special_recommend' => '特别推荐.0否,1.是',
            'flag_roll'              => '滚动.0否,1.是',
            'flag_bold'              => '加粗.0否,1.是',
            'flag_picture'           => '图片.0否,1.是',
            'created_at'             => '创建时间',
            'updated_at'             => '最后修改时间',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticleCategory()
    {
        return $this->hasOne(ArticleCategory::className(), ['id' => 'article_category_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticleContents()
    {
        return $this->hasMany(ArticleContent::className(), ['aid' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticleMeta()
    {
        return $this->hasMany(ArticleMetum::className(), ['aid' => 'id']);
    }

    /**
     * @Notes
     * @Interface getArticleTags
     *
     *
     * @return \yii\db\ActiveQuery
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2019-12-22 16:35
     */
    public function getArticleTags()
    {
        $tempModel = new ArticleMetaTag();

        return $this->hasMany(ArticleMetaLike::className(), ['aid' => 'id'])->where(['key' => $tempModel->keyName]);
    }

    /**
     * @Notes
     * @Interface getThumbUrlBySize
     *
     * @param string $width
     * @param string $height
     *
     * @return string|string[]
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2019-12-22 16:35
     */
    public function getThumbUrlBySize($width = '', $height = '')
    {
        if (empty($width) || empty($height)) {
            return $this->thumb;
        }
        if (empty($this->thumb)) {//未配图
            return $this->thumb = '/static/images/'.rand(1, 10).'.jpg';
        }
        static $str = null;
        if (null === $str) {
            $str = '';
            foreach (self::$thumbSizes as $temp) {
                $str .= $temp['w'].'x'.$temp['h'].'---';
            }
        }
        if (false !== strpos($str, $width.'x'.$height)) {
            $dotPosition = strrpos($this->thumb, '.');
            $thumbExt    = '@'.$width.'x'.$height;
            if (false === $dotPosition) {
                return $this->thumb.$thumbExt;
            } else {
                return substr_replace($this->thumb, $thumbExt, $dotPosition, 0);
            }
        }

        return Url::to(['/timthumb.php', 'src' => $this->thumb, 'h' => $height, 'w' => $width, 'zc' => 0]);
    }

    /**
     * @Notes
     * @Interface getArticleLikeCount
     *
     *
     * @return int|string
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2019-12-22 16:35
     */
    public function getArticleLikeCount()
    {
        return $this->getArticleLikes()->count('id');
    }

    /**
     * @Notes
     * @Interface getArticleLikes
     *
     *
     * @return \yii\db\ActiveQuery
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2019-12-21 21:04
     */
    public function getArticleLikes()
    {
        $tempModel = new ArticleMetaLike();

        return $this->hasMany(ArticleMetaLike::className(), ['aid' => 'id'])->where(['key' => $tempModel->keyName]);
    }

}
